var $sql = require('./userMapping');
var mysql = require('../../config/mysql');// 获取数据库连接配置

var config = require('../../config/config');//
var mapping = require('../mapping');//

var dateFormat = require('../../util/dateFormat');//

module.exports = {
    login: function (req, res, next) {
        // 获取前台页面传过来的参数
        var body = new config.reqBody(req);

        //必填参数是否传值
        if(body.userName == undefined || body.passWord == undefined){
            res.json({ code: config.code.parameterNull,'message': config.message.parameterNull+'userName passWord ',content:''});
            return;
        }
        //必填参数是否为空
        var userName=body.userName.trim(),passWord=body.passWord.trim();
        if(userName == null || passWord == null){
            res.json({ code: config.code.valNull,'message': config.message.valNull+'userName passWord ',content:''});
            return;
        }

        mysql.pool.getConnection(function(err, connection) {
            connection.query($sql.findName, [userName], function(err, result) {
                if(result.length > 0) {
                    connection.query($sql.findNameAndPass, [userName, passWord], function(err, result) {
                        if(result.length > 0) {
                            var userId=result[0].userId;
                            var nickName=result[0].nickName;
                            connection.query($sql.logTime, [dateFormat.format('yyyy-MM-dd hh:mm:ss'), userId], function(err, result) {
                                connection.release();
                                if (err) {
                                    console.log(err);
                                    res.json({ code: config.code.error,'message': err,content:''});
                                    return;
                                }
                                var user={
                                    userId:userId,
                                    userName:userName,
                                    passWord:passWord,
                                    nickName:nickName,
                                }
                                req.session.user=user;
                                console.log('登录成功');
                                //res.redirect('/index');
                                res.json({ code: config.code.success,'message': '登录成功',content:user});
                            });
                        }else{
                            connection.release();
                            res.json({ code: config.code.valError,'message': '用户名或密码错误',content:''});
                        }
                    });
                }else{
                    connection.release();
                    res.json({ code: config.code.valError,'message': '用户名不存在',content:''});
                }
            });
        });

    },
    register: function (req, res, next) {
        // 获取前台页面传过来的参数
        var body = new config.reqBody(req);

        //必填参数是否传值
        if(body.userName == undefined || body.passWord == undefined || body.nickName == undefined){
            res.json({ code: config.code.parameterNull,'message': config.message.parameterNull+'userName passWord nickName ',content:''});
            return;
        }
        var userName=body.userName || null,
            passWord=body.passWord || null,
            nickName=body.nickName || null,
            sex=body.sex || 2
        //必填参数是否为空
        if(userName == null || passWord == null || nickName == null){
            res.json({ code: config.code.valNull,'message': config.message.valNull+'userName passWord nickName ',content:''});
            return;
        }
        /* 设置字段名动态生成SQL和值 */
        var sqlObj=[
            {
                'name':'userName',
                'value':userName
            }, {
                'name':'passWord',
                'value':passWord
            }, {
                'name':'nickName',
                'value':nickName
            }, {
                'name':'sex',
                'value':sex
            }
        ];
        var sqlObj=mapping.insert('m_user',sqlObj);  //根据表名 字段名动态生成SQL(sql)和写入数据库的值(sqlVal)
        /* 设置字段名动态生成SQL和值 END*/

        mysql.pool.getConnection(function(err, connection) {
            connection.query($sql.findName, [userName], function(err, result) {
                if(result.length > 0) {
                    connection.release();
                    res.json({ code: config.code.valError,'message': '该帐号已经注册',content:''});
                }else{
                    connection.query(sqlObj.sql, sqlObj.sqlVal, function(err, result) {
                        connection.release();
                        if (err) {
                            console.log(err);
                            res.json({ code: config.code.error,'message': err,content:''});
                            return;
                        }
                        res.json({ code: config.code.success,'message': '注册成功',content:''});
                    });
                }
            });
        });
    },
    editId: function (req, res, next) {
        // 获取前台页面传过来的参数
        var body = new config.reqBody(req);

        //必填参数是否传值
        if(body.id == undefined){
            res.json({ code: config.code.parameterNull,'message': config.message.parameterNull+'id',content:''});
            return;
        }
        var id=body.id || null;
        //必填参数是否为空
        if(id == null){
            res.json({ code: config.code.valNull,'message': config.message.valNull+'id',content:''});
            return;
        }
        mysql.pool.getConnection(function(err, connection) {
            connection.query($sql.findId, [id], function(err, result) {
                connection.release();
                if (err) {
                    console.log(err);
                    res.json({ code: config.code.error,'message': err,content:''});
                    return;
                }
                res.json({ code: config.code.success,'message': config.message.success,content:result});
            });
        });

    },
    edit: function (req, res, next) {
        // 获取前台页面传过来的参数
        var body = new config.reqBody(req);

        //必填参数是否传值
        if(body.id == undefined){
            res.json({ code: config.code.parameterNull,'message': config.message.parameterNull+'id ',content:''});
            return;
        }
        var id=body.id || null,
            nickName=body.nickName || null,
            sex=body.sex || null;
        //必填参数是否为空
        if(id == null){
            res.json({ code: config.code.valNull,'message': config.message.valNull+'id ',content:''});
            return;
        }
        if(nickName==null&&sex==null){
            res.json({ code: config.code.success,'message': '没有信息可修改',content:''});
            return;
        }
        /* 设置字段名动态生成SQL和值 */
        var sqlObj=[
            {
                'name':'userId',
                'value':id
            }, {
                'name':'nickName',
                'value':nickName
            }, {
                'name':'sex',
                'value':sex
            }
        ];
        var sqlObj=mapping.update('m_user',sqlObj);  //根据表名 字段名动态生成SQL(sql)和写入数据库的值(sqlVal)
        /* 设置字段名动态生成SQL和值 END*/
        mysql.pool.getConnection(function(err, connection) {
            connection.query(sqlObj.sql, sqlObj.sqlVal, function(err, result) {
                connection.release();
                if (err) {
                    console.log(err);
                    res.json({ code: config.code.error,'message': err,content:''});
                    return;
                }
                res.json({ code: config.code.success,'message': '修改成功',content:result});
            });
        });

    },
};